<template>
  <a-modal
    title="修改终端"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['clientId',{rules: [{required: true, message: '请输入编号'}]}]"
                :disabled="showable || editable"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="密钥"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['clientSecret',{rules: [{required: true, message: '请输入密钥'}]}]"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>

        <a-row>
          <a-col :span="12">
            <a-form-item
              label="域"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['scope',{rules: [{required: true, message: '请输入域'}]}]"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="授权方式"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['authorizedGrantTypes',{rules: [{required: true, message: '请输入授权方式'}]}]"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="回调地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['webServerRedirectUri']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="权限"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['authorities']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="自动放行"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-switch
                checkedChildren="是"
                :disabled="showable"
                unCheckedChildren="否"
                v-decorator="['autoapprove', {initialValue: true,valuePropName: 'checked',rules: [{required: true, message: '选择授权方式'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="令牌时效"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['accessTokenValidity']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="刷新时效"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['refreshTokenValidity']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="扩展信息"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['additionalInformation']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col :span="12">
            <a-form-item
              label="资源ID"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-decorator="['resourceIds']"
                :disabled="showable"
              />
            </a-form-item>
          </a-col>
        </a-row>

      </a-form>
    </a-spin>
    <template slot="footer" >
      <div
        v-show="editable || addable"
      >
        <a-button key="cancel" @click="handleCancel">取消</a-button>
        <a-button key="forward" :loading="confirmLoading" type="primary" @click="handleOk">保存</a-button>
      </div>
    </template>
  </a-modal>

</template>

<script>
import pick from 'lodash.pick'
import { getObj, addObj, putObj } from '@api/sys/client'
export default {
  name: 'UserAddOrUpdate',
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      editable: false,
      addable: false,
      showable: false,
      type: 'add'
    }
  },
  methods: {
    // 获取用户信息
    edit (id, type) {
      this.visible = true
      if (type && type === 'add') {
        this.addable = true
        this.type = type
      }
      if (type === 'edit') {
        this.editable = true
        this.type = type
      }
      if (type === 'show') {
        this.showable = true
        this.type = type
      }
      const { form: { setFieldsValue, resetFields } } = this
      if (['edit', 'show'].includes(type)) {
        this.confirmLoading = true
        getObj(id).then(res => {
          const record = res.data
          if (record.autoapprove === 'true') {
            record.autoapprove = true
          } else {
            record.autoapprove = false
          }
          this.confirmLoading = false
          this.$nextTick(() => {
            setFieldsValue(pick(record, ['clientId', 'clientSecret', 'resourceIds', 'scope', 'authorizedGrantTypes', 'webServerRedirectUri',
              'authorities', 'accessTokenValidity', 'refreshTokenValidity', 'additionalInformation', 'autoapprove']))
          })
        })
      } else {
        resetFields()
      }
    },
    // 结束保存用户信息
    handleOk () {
      this.form.validateFields(async (err, values) => {
        if (!err) {
          this.confirmLoading = true
          try {
            if (this.type === 'add') {
              await addObj(values)
            } else if (this.type === 'edit') {
              await putObj(values)
            }
          } catch (err) {
            this.$message.error(err.msg)
          }
          setTimeout(() => {
            this.confirmLoading = false
            this.$emit('ok')
            this.visible = false
          }, 1500)
        }
      })
    },
    handleCancel () {
      this.visible = false
      setTimeout(() => {
        this.addable = false
        this.showable = false
        this.editable = false
      }, 200)
    }
  }
}
</script>

<style scoped>

</style>
